const MOD_DEVICE='big_log';//默认表

/**
 * 运行日志
 */
module.exports = class Log extends require('./model_pool')
{
  //新增记录
  static newDevLog(uid, hwid, sn, msg, stamp) {
    return new Promise((resolve,reject) => {
      let sql = 'INSERT INTO `'+MOD_DEVICE+'` (type,uid,hwid,sn,msg,stamp) VALUES(?,?,?,?,?,?)';
      this.query(sql,['dev',uid,hwid,sn,msg,stamp]).then((res)=>{
        resolve(res.insertId);
      }).catch(err=>{
        console.log('[%d]['+MOD_DEVICE+'] newDevLog faild:'+err.message,hwid);
        reject(err);
      })
    })
  }

  static newUserLog(uid,hwid,sn,msg,stamp){
    return new Promise((resolve,reject) => {
      let sql = 'INSERT INTO `'+MOD_DEVICE+'` (type,uid,hwid,sn,msg,stamp) VALUES(?,?,?,?,?,?)';
      this.query(sql,['user',uid,hwid,sn,msg,stamp]).then((res)=>{
        resolve(res.insertId);
      }).catch(err=>{
        console.log('[%d]['+MOD_DEVICE+'] newUserLog faild:'+err.message,uid);
        reject(err);
      })
    })
  }

  //删除指定时间戳之前的数据
  static cleanByStamp(stamp){
    return new Promise((resolve,reject) => {
      let sql = 'DELETE FROM `'+MOD_DEVICE+'` where stamp<=?';
      this.query(sql,[stamp]).then(res=>{
        console.log('['+MOD_DEVICE+'] clean log',res.affectedRows,'rows');
        resolve(res.affectedRows);
      }).catch(err=>{
        console.log('['+MOD_DEVICE+'] clean log faild:'+err.message);
        reject(err);
      })
    })
  }


}